package net.jueb.sniff4j.impl.http.ui;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import net.jueb.sniff4j.extr.base.AbstractRuntime;
import net.jueb.sniff4j.extr.base.BootConfig;
import net.jueb.sniff4j.extr.sendHandler.SendToTargetHandler;
import net.jueb.sniff4j.impl.http.ui.uiLog.normalThread.JTextAreaLogAppenderThread;
import net.jueb.sniff4j.socksCroe.SocksServer;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JTextArea;
import javax.swing.JScrollPane;

import org.apache.log4j.Logger;

import java.awt.Color;

public class MainUi extends JFrame {
	protected static final InternalLogger log = InternalLoggerFactory.getInstance(SendToTargetHandler.class);
	public SocksServer socksServer=new SocksServer();
	public AbstractRuntime runTime;
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -2625644533847538476L;
	private JPanel contentPane;
	private JTextField txt_ServerIP;
	private JLabel label;
	private JLabel label_1;
	private JTextField txt_ServerPort;
	private JButton btn_StartServer;
	private final JTextArea txt_Log;
	private final JScrollPane scrollPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				try {
					MainUi frame = new MainUi();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		
	}
	public void setRunTime(AbstractRuntime runtime)
	{
		this.runTime=runtime;
	}
	/**
	 * Create the frame.
	 */
	public MainUi() {
		setResizable(false);
		setStyle();
		setTitle("sniff4j");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 582, 501);
		contentPane = new JPanel();
		contentPane.setToolTipText("");
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		label = new JLabel("绑定地址：");
		label.setBounds(10, 10, 66, 15);
		contentPane.add(label);
		
		txt_ServerIP = new JTextField();
		txt_ServerIP.setText("0.0.0.0");
		txt_ServerIP.setBounds(69, 7, 118, 21);
		contentPane.add(txt_ServerIP);
		txt_ServerIP.setColumns(10);
		
		label_1 = new JLabel("端口：");
		label_1.setBounds(197, 10, 42, 15);
		contentPane.add(label_1);
		
		txt_ServerPort = new JTextField();
		txt_ServerPort.setText("1080");
		txt_ServerPort.setBounds(243, 7, 54, 21);
		contentPane.add(txt_ServerPort);
		txt_ServerPort.setColumns(10);
		
		btn_StartServer = new JButton("启动代理服务器");
		btn_StartServer.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(btn_StartServer.getText().equals("启动代理服务器"))
				{
					runServer();
					btn_StartServer.setText("停止代理服务器");
				}else
				{
					stopServer();
					btn_StartServer.setText("启动代理服务器");
				}
			}
		});
		btn_StartServer.setBounds(317, 6, 118, 23);
		contentPane.add(btn_StartServer);
		
		scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 48, 555, 415);
		contentPane.add(scrollPane);
		
		txt_Log = new JTextArea();
		txt_Log.setEditable(false);
		txt_Log.setForeground(Color.GREEN);
		txt_Log.setBackground(Color.BLACK);
		txt_Log.setWrapStyleWord(true);
		txt_Log.setLineWrap(true);
		scrollPane.setViewportView(txt_Log);
		
		JButton btn_clearLog = new JButton("清除日志");
		btn_clearLog.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				txt_Log.setText("");
			}
		});
		btn_clearLog.setBounds(451, 6, 93, 23);
		contentPane.add(btn_clearLog);
		init();
	}
	private void init()
	{
		initUiLog();
	}

	private void runServer()
	{
		if(runTime!=null)
		{
			int port=Integer.parseInt(this.txt_ServerPort.getText());
			BootConfig boot=runTime.getBootConfig();
			boot.setProxyServerIp(this.txt_ServerIP.getText());
			boot.setProxyServerPort(port);
			boot.setAuth(true);
			socksServer.run(runTime);
//			new SocksServer().runByOiomode(runTime);
		}
	}
	private void stopServer()
	{
		socksServer.stop();
	}
	private void setStyle()
	{
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (InstantiationException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (UnsupportedLookAndFeelException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} 
	}
	private void initUiLog()
	{
//		JTextAreaLogAppender jt=new JTextAreaLogAppender("textArea", "[%d{yyyy/MM/dd HH:mm:ss SSS}][%p]:%n%m%n", txt_Log,scrollPane);
//		jt.initRootLogger(jt);
		Logger.getRootLogger().removeAllAppenders();
		JTextAreaLogAppenderThread jtt=new JTextAreaLogAppenderThread("textArea", "[%d{yyyy/MM/dd HH:mm:ss SSS}][%p]:%n%m%n", txt_Log,scrollPane);
		jtt.start();
	}
}
